﻿using TamoCRM.Core.Caching;
using TamoCRM.Core.Commons.Utilities;
using TamoCRM.Core.Data;

namespace TamoCRM.Services.DuplicateChecking
{
    public class RedisDuplicateCheckingProvider : CheckDuplicateProvider 
    {
        public override bool IsDuplicate(string mobile1, string mobile2, string tel, string email, string email2, int contactId, out string info)
        {
            int contactIdDb;
            info = string.Empty;
            string contactInfoDb;
            var check = IsDuplicate(mobile1, mobile2, tel, email, email2, out contactIdDb, out contactInfoDb);
            if (check)
            {
                if (contactId != contactIdDb)
                {
                    info = contactInfoDb;
                    return true;
                }
                return false;
            }
            return false;
        }

        public override bool IsDuplicate(string mobile1, string mobile2, string tel, string email, string email2, out int contactId, out string info)
        {
            int retVal;
            contactId = 0;
            info = string.Empty;
            if (!string.IsNullOrEmpty(mobile1))
            {
                retVal = ConvertHelper.ToInt32(CachingProvider.Instance().Get(mobile1));
                if (retVal != 0) 
                {
                    info = mobile1;
                    contactId = retVal;
                    return true;
                }
            }
            if (!string.IsNullOrEmpty(mobile2))
            {
                retVal = ConvertHelper.ToInt32(CachingProvider.Instance().Get(mobile2));
                if (retVal != 0)
                {
                    info = mobile2;
                    contactId = retVal;
                    return true;
                }
            }
            if (!string.IsNullOrEmpty(tel))
            {
                retVal = ConvertHelper.ToInt32(CachingProvider.Instance().Get(tel));
                if (retVal != 0)
                {
                    info = tel;
                    contactId = retVal;
                    return true;
                }
            }
            if (!string.IsNullOrEmpty(email))
            {
                retVal = ConvertHelper.ToInt32(CachingProvider.Instance().Get(email));
                if (retVal != 0)
                {
                    info = email;
                    contactId = retVal;
                    return true;
                }
            }
            if (!string.IsNullOrEmpty(email2))
            {
                retVal = ConvertHelper.ToInt32(CachingProvider.Instance().Get(email2));
                if (retVal != 0)
                {
                    info = email2;
                    contactId = retVal;
                    return true;
                }
            }
            return false;
        }

        public override int IsDuplicate(string mobile1, string mobile2, string tel, string email, string email2)
        {
            int contactIdDb;
            string contactInfoDb;
            IsDuplicate(mobile1, mobile2, tel, email, email2, out contactIdDb, out contactInfoDb);
            return contactIdDb;
        }

        public override int IsDuplicate(string mobile1, string mobile2, string tel, string email, string email2, int contactId)
        {
            int contactIdDb;
            string contactInfoDb;
            var check = IsDuplicate(mobile1, mobile2, tel, email, email2, out contactIdDb, out contactInfoDb);
            return check ? (contactId != contactIdDb ? contactIdDb : 0) : 0;
        }
    }
}
